import requests
import re
from lxml import etree
from io import BytesIO
import base64
from fontTools.ttLib import TTFont
from xml.dom.minidom import parse
from io import BytesIO

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
}

font_dict = {
    (0,410)      :0,
    (-888, 0)    :1,
    (-942, 0)    :2,
    (159, -123)  :3,
    (-198, 0)    :4,
    (156, -102)  :5,
    (-121, 62)   :6,
    (-606, -1481):7,
    (-277, 129)  :8,
    (128, -74)   :9,
}

url = "https://sz.58.com/chuzu/?PGTID=0d200001-0000-4ae6-d84a-bb7dbe4393f3&ClickID=1"

html = requests.get(url,headers=headers)

# 从不经过解码的源代码中提取base64加密后的编码
font_encoded_data = re.search('base64,(.*?)\)', html.text, re.S).group(1)
font_decoded_data = base64.b64decode(font_encoded_data)

ttf_file_name = 'font_58.woff'
with open(ttf_file_name, 'wb') as fp:
    fp.write(font_decoded_data)

fonts = TTFont(ttf_file_name)
fonts.saveXML("movie.xml")


tf = TTFont(BytesIO(font_decoded_data))

print(tf['glyf'].glyphs.get('glyph00002').data)

glyph_dict = {}

for i in tf.getGlyphNames()[1:]:
    x1,y1 = tf["glyf"][i].coordinates[0][0],tf["glyf"][i].coordinates[0][1]
    x2,y2 = tf["glyf"][i].coordinates[1][0],tf["glyf"][i].coordinates[1][1]
    new=(x2-x1,y2-y1)
    glyph_dict[i] = new
    print(i,new)



print(xml_dict)
print(glyph_dict)